﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Catering.yl
{
    public partial class EquipmentAndFacility : System.Web.UI.Page
    {        
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!Page.IsPostBack)
            {               
                LoadForm("");
                LoadDMMAccountDropDownList();                            
            }
        }
        //public void LoadForm()
        //{
        //    if( Session["useDate"] != null && Session["ClassID"] != null )
        //    {
        //        try
        //        {
        //            DataTable dt_Plan = new Catering_Rules.EquipmentAndFacility().GetList("ClassID = "+Session["ClassID"].ToString()).Tables[0];
        //            dt_Plan.Columns.Add("tempId");
        //            dt_Plan.Columns.Add("tempName");
        //            Catering_Rules.DMMAccount _rules = new Catering_Rules.DMMAccount();
        //            for (int i = 0; i < dt_Plan.Rows.Count; i++)
        //            {
        //                dt_Plan.Rows[i]["tempId"] = i + 1;
        //                try
        //                {
        //                    int pId = Convert.ToInt32(dt_Plan.Rows[i]["PName"]);
        //                    var model = _rules.GetModel(pId);
        //                    dt_Plan.Rows[i]["tempName"]=model.PName;
        //                }
        //                catch
        //                {
        //                    continue;
        //                }

        //            }
        //            this.Rep_Plan.DataSource = dt_Plan;
        //            this.Rep_Plan.DataBind();
        //        }
        //        catch
        //        {

        //        }
        //    }            
        //}

        public void LoadForm(string DMMAccountID)
        {
            string where = " 1 =1 ";
            if (!string.IsNullOrEmpty(DMMAccountID))
            {
                where += " and PType = '" + DMMAccountID + "'";
            }
            if (Session["useDate"] != null && Session["ClassID"] != null)
            {
                try
                {
                    where += " and ClassID = " + Session["ClassID"].ToString();
                    DataTable dt_Plan = new Catering_Rules.EquipmentAndFacility().GetList(where).Tables[0];
                    dt_Plan.Columns.Add("tempId");
                    dt_Plan.Columns.Add("tempName");
                    Catering_Rules.DMMAccount _rules = new Catering_Rules.DMMAccount();
                    for (int i = 0; i < dt_Plan.Rows.Count; i++)
                    {
                        dt_Plan.Rows[i]["tempId"] = i + 1;
                        try
                        {
                            int pId = Convert.ToInt32(dt_Plan.Rows[i]["PName"]);
                            var model = _rules.GetModel(pId);
                            dt_Plan.Rows[i]["tempName"] = model.PName;
                        }
                        catch
                        {
                            continue;
                        }
                        for (int Cols = 0; Cols < dt_Plan.Columns.Count; Cols++)
                        {
                            if (dt_Plan.Rows[i][Cols].ToString() == "")
                                dt_Plan.Rows[i][Cols] = "&nbsp";
                        }
                    }
                    this.Rep_Plan.DataSource = dt_Plan;
                    this.Rep_Plan.DataBind();
                }
                catch
                {

                }
            }
        }


        public void LoadDMMAccountDropDownList()
        {
            try
            {
                Catering_Rules.DMMAccount _rules = new Catering_Rules.DMMAccount();
                var list = _rules.GetModelList("ClassID = " + Session["ClassID"].ToString());
                ddlDMMAccount.Items.Add(new ListItem("全部", ""));
                ddlDMMAccount.Items.Add(new ListItem("关键设备", "关键设备"));
                ddlDMMAccount.Items.Add(new ListItem("重要设备", "重要设备"));
                ddlDMMAccount.Items.Add(new ListItem("一般设备", "一般设备"));
            }
            catch
            {
            }
        }

        protected void CustomerPropertyList_Click(object sender, EventArgs e)
        {
            string strOpenUrl = "window.open('Top_Daily.aspx?mainurl=CustomerPropertyList.aspx','newwindow')";
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strOpenUrl, true);
        }
        protected void DMMAccount_Click(object sender, EventArgs e)
        {
            string strOpenUrl = "window.open('Top_Daily.aspx?mainurl=DMMAccount.aspx','newwindow')";
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strOpenUrl, true);
        }
        [WebMethod]         
        public static string Add(string PName, string PType, string Person, string zhouqi, string hdn_useDate,string hdn_currentClassID)
        {
            Catering_Model.EquipmentAndFacility _ce = new Catering_Model.EquipmentAndFacility();
            _ce.PName = PName;
            _ce.PType = PType;
            _ce.Person = Person;
            _ce.zhouqi = zhouqi;
            _ce.ClassId = int.Parse(hdn_currentClassID);
            _ce.SaveDate = Convert.ToDateTime(hdn_useDate);
            new Catering_Rules.EquipmentAndFacility().Add(_ce);
            return "OK";
        }
        protected void Rep_Plan_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                int delID = Convert.ToInt32(e.CommandArgument);
                if (new Catering_Rules.EquipmentAndFacility().Delete(delID))
                {
                    this.LoadForm("");
                }
            }
            if (e.CommandName == "SpareParts")
            {
                Session["SapreParts"] = e.CommandArgument.ToString();
                string strOpenUrl = "window.open('Top_Daily.aspx?mainurl=SpareParts.aspx','newwindow')";
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strOpenUrl, true);
            }
        }
        [WebMethod]
        public static ArrayList Get(string _id)
        {
            ArrayList al = new ArrayList();
            Catering_Model.EquipmentAndFacility md = new Catering_Rules.EquipmentAndFacility().GetModel(Convert.ToInt32(_id));
            al.Add(md.Id);
            al.Add(md.PName);
            al.Add(md.PType);
            al.Add(md.Person);
            al.Add(md.zhouqi);
            return al;
        }
        [WebMethod]
        public static string Edit(string _id, string PName, string PType, string Person, string zhouqi)
        {
            int id = 0;
            int areaid = 0;
            Int32.TryParse(_id, out id);
            if (id <= 0) return "NO";

            Catering_Model.EquipmentAndFacility _CE = new Catering_Model.EquipmentAndFacility();

            _CE.Id = id;
            _CE.PName = PName;
            _CE.PType = PType;
            _CE.Person = Person;
            _CE.zhouqi = zhouqi;
            new Catering_Rules.EquipmentAndFacility().Update(_CE);

            return "OK";
        }
        [WebMethod]
        public static ArrayList GetPName(string classID)
        {
            System.Collections.ArrayList al = new System.Collections.ArrayList();
            if (!string.IsNullOrEmpty(classID))
            {
                Catering_Rules.DMMAccount _rules = new Catering_Rules.DMMAccount();
                var list = _rules.GetModelList("ClassID = " + classID);
                foreach (var item in list)
                {
                    al.Add(item.Id + "," + item.PName);
                }
            }
            return al;
        }

        protected void ddlDMMAccount_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                LoadForm(ddlDMMAccount.SelectedValue);
            }
            catch
            {

            }
        }
    }
}